package com.rahms.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rahms.pojo.entity.DrugPlan;
import com.rahms.server.mapper.DrugPlanMapper;
import com.rahms.server.service.DrugPlanService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * 用药计划服务实现类
 */
@Service
public class DrugPlanServiceImpl extends ServiceImpl<DrugPlanMapper, DrugPlan> implements DrugPlanService {
    
    @Override
    public List<DrugPlan> getDrugPlansByElderlyId(Long elderlyId) {
        LambdaQueryWrapper<DrugPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DrugPlan::getElderlyId, elderlyId);
        return this.list(queryWrapper);
    }
    
    @Override
    public List<DrugPlan> getActiveDrugPlans() {
        LocalDate now = LocalDate.now();
        LambdaQueryWrapper<DrugPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DrugPlan::getStatus, 1)  // 状态为生效中
                   .le(DrugPlan::getStartDate, now)  // 开始日期小于等于今天
                   .and(wrapper -> wrapper.isNull(DrugPlan::getEndDate).or().ge(DrugPlan::getEndDate, now));  // 结束日期为空或大于等于今天
        return this.list(queryWrapper);
    }
}